<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/12/30
 * Time: 19:57
 */

namespace system\core\config;


class Config
{
    //配置文件信息
    public static $configmsg = array();
    
    
    
    public static function load(){
        self::loadsysconfig();//加载系统配置文件
        self::loadcommonconfig();//加载外部公共配置文件
        self::loadsyshelp();//加载公共函数
    }
    
    //加载系统配置文件
    public static function loadsysconfig()
    {
        
        $configdir = SYSTEM_PATH.'config';
        if(is_dir($configdir)){
            $handle = @opendir($configdir);
            while (false !== ($file_path = readdir($handle))){
                if($file_path!='.'&&$file_path!='..'){
                    //遍历文件
                    $conarr = include $configdir.DS.$file_path;
                    if(is_array($conarr)){
                        self::$configmsg = array_merge(self::$configmsg,$conarr) ;
                    }
                }
            }
        }
    }

    //加载外部公共配置文件
    public static function loadcommonconfig()
    {

        $configdir =rtrim(CONF_PATH,DS) ;
        if(is_dir($configdir)){
            $handle = @opendir($configdir);
            while (false !== ($file_path = readdir($handle))){
                if($file_path!='.'&&$file_path!='..'){
                    //遍历文件
                    $conarr = include $configdir.DS.$file_path;
                    if(is_array($conarr)){
                        self::$configmsg = array_merge(self::$configmsg,$conarr) ;
                    }
                }
            }
        }
    }
    
    /*
     * 设置配置
     * @param  stirng $name
     * @param  mixed $value
     * */
    public static function set($name,$value){
        self::$configmsg[$name] = $value;
    }


    /*
     * 设置配置
     * @param  stirng $name
     * @param  mixed $value
     * */
    public static function get($name){
        if (empty(self::$configmsg[$name])){
            return null;
        }else{
            return self::$configmsg[$name] ;
        }
       
    }
    
    
    /*
     * 
     * 加载系统函数
     * 
     * 
     * 
     * */
    public static function loadsyshelp(){
        if ( !empty(self::$configmsg['help']) && is_array(self::$configmsg['help'])  ){

            //公共函数配置不为空
            foreach (self::$configmsg['help'] as $item => $value){
                
                if (!empty($value) && is_array($value)){
                    foreach ($value as $file){
                        if (file_exists(ROOT_PATH.$file)){
                            include ROOT_PATH.$file;
                        }
                    }
                }
            }
        }
    }

     
    
}